package com.fsh.anime.controller;

import com.fsh.anime.model.dtos.FanjuDto;
import com.fsh.anime.model.dtos.PageFanjuDto;
import com.fsh.anime.model.dtos.PageFanjuDto2;
import com.fsh.anime.model.vos.Result;
import com.fsh.anime.service.FanjuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;



@RestController
@Slf4j
@RequestMapping("/api/Animes")
public class FanjuController {
    @Autowired
    private FanjuService fanjuService;

    /**
     * 分页获取动漫列表
     *
     * @param dto dto
     * @return {@link Result}
     */
    @PostMapping("/getAnimes")
    public Result getAnimes(@RequestBody PageFanjuDto dto){
        log.info("PageFanjuDto,{}",dto);
        return fanjuService.getAnimes(dto);
    }

    /**
     * 添加动画
     *
     * @param dto dto
     * @return {@link Result}
     */
    @PostMapping("/addAnime")
    public Result addAnime(@RequestBody @Validated FanjuDto dto, BindingResult results){
        if (results.hasErrors()){
            return Result.error(results.getFieldError().getDefaultMessage());
        }
        log.info("番剧，{}",dto);
        return fanjuService.addAnime(dto);
    }

    /**
     * 更新动漫状态
     *
     * @param id     id
     * @param status 状态
     * @return {@link Result}
     */
    @PutMapping("/updateAnimeStatus")
    public Result updateAnimeStatus(Integer id,Integer status){
        return fanjuService.updateAnimeStatus(id,status);
    }

    /**
     * 删除动漫
     *
     * @param id id
     * @return {@link Result}
     */
    @DeleteMapping("/delAnime/{id}")
    public Result delAnime(@PathVariable Integer id,String cover){
        log.info("cover,{}",cover);
        return fanjuService.delAnime(id,cover);
//        return null;
    }

    /**
     * 上传图片
     *
     * @param file 文件
     * @return {@link String}
     */
    @PostMapping("/uploadImg")
    public String upload(MultipartFile file){
        return fanjuService.upload(file);
    }

//----------------------网站----------------------
    /**
     * 得到所有番剧
     *
     * @return {@link Result}
     */
    @GetMapping("/getAllFanju")
    public Result getAllFanju(PageFanjuDto2 dto,BindingResult results){
        if (results.hasErrors()){
            return Result.error(results.getFieldError().getDefaultMessage());
        }
        return fanjuService.getAllFanju(dto);
    }

}
